home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / microcrn / issue_46.arc / ISR.ARC / CALL.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-11  |  6.6 KB  |  278 lines

  1. /*    CALL.h   -  header to allow low level routines to
  2.                 look like they work directly with CPU
  3.                 registers and do callf/ints "naturally!"
  4.                 Works with the functions in CALL.C/ISR.*
  5.  
  6.         Supports ISR article in Micro Cornucopia Magazine Issue #46
  7. */
  8.  
  9.  
  10. #ifndef OP_RETF
  11. #include <cpu.h>
  12. #endif
  13.  
  14.  
  15. /*    These typedefs describe the types of structures that are found
  16.     in the final register structure at the end.
  17.  
  18.     These aren't intended to be used by humans...
  19. */
  20.  
  21. typedef union __REG {
  22.    struct { 
  23.       unsigned char l, h; 
  24.    } b;
  25.    unsigned int  x;
  26. } __reg;
  27.  
  28. typedef union __PCREG {
  29.    void far (*x)();
  30.    struct { 
  31.       unsigned int l, h; 
  32.    } w;
  33. } __pcreg;
  34.  
  35. typedef union __REGDP {
  36.    struct { 
  37.       __reg r; 
  38.       unsigned int s; 
  39.    } i;
  40.    unsigned char far *p;
  41.    unsigned long l;
  42. } __regdp;
  43.  
  44. typedef union __REGPSW {
  45.    __flags bit;
  46.    unsigned int w;
  47. } __regpsw;
  48.  
  49. typedef union __REG3R {
  50.    struct {
  51.       __reg ax, dx;
  52.       unsigned int ds;
  53.    } r1;
  54.    struct {
  55.       __reg ax;
  56.       __regdp dsdx;
  57.    } r2;
  58.    struct {
  59.       __regdp dxax;
  60.       unsigned int ds;
  61.    } r3;
  62. } __reg3r;
  63.  
  64.  
  65. /*    This is the main register file structure. The interrupt handlers
  66.     all push and pop registers in this order, and the functions
  67.     in CALL.C work with this "register file" organization. Note that 
  68.     the stack pointer isn't actually used. That object was included 
  69.     in the structure for "completeness" and/or "future expansion,"
  70.     etc. 
  71. */
  72.  
  73. typedef struct __REGS {
  74.   __regdp esbx;
  75.   __reg c;
  76.   __reg3r dsdxax;
  77.   unsigned int di;
  78.   __regdp bpsi;
  79.   __pcreg pc;
  80.   __regpsw psw;
  81.   __regdp stk;
  82. } __regs;
  83.  
  84. /*    These macros are provided to make it easier to access
  85.     objects in the above structure. Use either p->AX or s.AX
  86.     as appropriate to keep the code from looking too silly...
  87. */
  88.  
  89. #define AX       dsdxax.r1.ax.x
  90. #define AH       dsdxax.r1.ax.b.h
  91. #define AL       dsdxax.r1.ax.b.l
  92.  
  93. #define BX       esbx.i.r.x
  94. #define BH       esbx.i.r.b.h
  95. #define BL       esbx.i.r.b.l
  96.  
  97. #define CX       c.x
  98. #define CH       c.b.h
  99. #define CL       c.b.l
  100.  
  101. #define DX       dsdxax.r1.dx.x
  102. #define DH       dsdxax.r1.dx.b.h
  103. #define DL       dsdxax.r1.dx.b.l
  104.  
  105. #define DI       di
  106.  
  107. #define SI       bpsi.i.r.x
  108. #define BP       bpsi.i.s
  109.  
  110. #define DS       dsdxax.r1.ds
  111. #define ES       esbx.i.s
  112.  
  113. #define BPSI     bpsi.p
  114. #define DXAX     dsdxax.r3.dxax.l
  115. #define DSDX     dsdxax.r2.dsdx.p
  116. #define ESBX     esbx.p
  117.  
  118. #define PSW      psw.w
  119.  
  120. #define PC       pc.x
  121. #define CS       pc.w.h
  122. #define IP       pc.w.l
  123.  
  124. #define STKPTR   stk.p
  125. #define SS       stk.i.s
  126. #define SP       stk.i.r.x
  127.  
  128. #define OF     psw.bit.of     
  129. #define DIR     psw.bit.dir     
  130. #define INTE     psw.bit.inte 
  131. #define TRAP     psw.bit.trap 
  132. #define SIGN     psw.bit.sign 
  133. #define ZERO     psw.bit.zero 
  134. #define AUXC     psw.bit.auxc 
  135. #define PE     psw.bit.pe     
  136. #define CARRY    psw.bit.carry
  137.  
  138.  
  139. /*    The macros here all refer to something called _REGS,
  140.     which must be defined in your module. It can either 
  141.     be a structure, or another #defined macro which 
  142.     refers to a structure, etc.
  143.  
  144.     The idea here is that modules that contain many DOS or 
  145.     BIOS calls can put a __regs _REGS; declaration at the 
  146.     start of every function. As such, _AX allows access to 
  147.     the AX object in the structure, etc.
  148.  
  149.     This makes the source code look MUCH better!
  150. */
  151.  
  152.  
  153. #define _REGSP   ((__regs far *)&(_REGS))
  154.  
  155. #define _AX      (_REGS.AX)
  156. #define _AH      (_REGS.AH)
  157. #define _AL      (_REGS.AL)
  158.  
  159. #define _BX      (_REGS.BX)
  160. #define _BH      (_REGS.BH)
  161. #define _BL      (_REGS.BL)
  162.  
  163. #define _CX      (_REGS.CX)
  164. #define _CH      (_REGS.CH)
  165. #define _CL      (_REGS.CL)
  166.  
  167. #define _DX      (_REGS.DX)
  168. #define _DH      (_REGS.DH)
  169. #define _DL      (_REGS.DL)
  170.  
  171. #define _SI      (_REGS.SI)
  172. #define _DI      (_REGS.DI)
  173. #define _BP      (_REGS.BP)
  174.  
  175. #define _DS      (_REGS.DS)
  176. #define _ES      (_REGS.ES)
  177.  
  178. #define _BPSI    (_REGS.BPSI)
  179. #define    _DXAX    (_REGS.DXAX)
  180. #define _DSDX    (_REGS.DSDX)
  181. #define _ESBX    (_REGS.ESBX)
  182.  
  183. #define _PSW     (_REGS.PSW)
  184.  
  185. #define _PC      (_REGS.PC)
  186. #define _IP      (_REGS.IP)
  187. #define _CS      (_REGS.CS)
  188.  
  189. #define _STKPTR  (_REGS.STKPTR)
  190. #define _SS      (_REGS.SS)
  191. #define _SP      (_REGS.SP)
  192.  
  193. #define _OF      (_REGS.OF)
  194. #define _DIR     (_REGS.DIR)
  195. #define _INTE    (_REGS.INTE)
  196. #define _TRAP    (_REGS.TRAP)
  197. #define _SIGN    (_REGS.SIGN)
  198. #define _ZERO    (_REGS.ZERO)
  199. #define _AUXC    (_REGS.AUXC)
  200. #define _PE      (_REGS.PE)
  201. #define _CARRY   (_REGS.CARRY)
  202.  
  203.  
  204. /*    The following functions are in the module CALL.C.
  205.     Call_8086() calls the far function at _PC, while
  206.     intn_8086() expects an interrupt number to be in
  207.     _IP. Seg86 simply fills the __r structures' segment
  208.     registers with the values found in the CPU registers
  209.     at the time of the call.
  210. */
  211.  
  212. unsigned int far seg86( __regs far *ri );
  213. unsigned int far call_8086( __regs far *ri, __regs far *ro );
  214. unsigned int far intn_8086( __regs far *ri, __regs far *ro );
  215. void __regs_dump(__regs far *rp);
  216.  
  217.  
  218. /*    These macros do what they seem to do...
  219.  
  220.     NOTE: the cast to (void far (*)()) for _PC in the
  221.     call86() macro doesn't work unless x appears before
  222.     l,h in the typedef for PCREG (Manx 'C 86, V. 4.10a.)
  223. */
  224.  
  225. #define call86(pc)  ( _PC = (pc), call_8086( (_REGSP), (_REGSP) ) )
  226. #define int86(i)    ( _IP = ( i), intn_8086( (_REGSP), (_REGSP) ) )
  227. #define pcdos(f)    ( _AH = ( f), int86( 0x21 )                   )
  228.  
  229.  
  230. #asm
  231.     ;If a 'C function is declared as being far, and having two
  232.     ;far pointers (_RI and _RO) to __regs structures as its 
  233.     ;first two parameters, the instruction "les bx, _RI" would 
  234.     ;set es:bx to point to the _RI __regs structure, and
  235.     ;"les bx, _RO" would set es:bx to point to the _RO __regs
  236.     ;structure.
  237.     ;
  238. _RI    equ   dword ptr  6[bp]
  239. _RO    equ   dword ptr 10[bp]
  240.  
  241.     ;The following are offsets from ES:BX to the contents
  242.     ;of the __regs structure.
  243.     ;
  244. _BX    equ   es:word ptr 00[bx]
  245. _BL    equ   es:byte ptr 00[bx]
  246. _BH    equ   es:byte ptr 01[bx]
  247.  
  248. _ES    equ   es:word ptr 02[bx]
  249.  
  250. _CX    equ   es:word ptr 04[bx]
  251. _CL    equ   es:byte ptr 04[bx]
  252. _CH    equ   es:byte ptr 05[bx]
  253.  
  254. _AX    equ   es:word ptr 06[bx]
  255. _AL    equ   es:byte ptr 06[bx]
  256. _AH    equ   es:byte ptr 07[bx]
  257.  
  258. _DX    equ   es:word ptr 08[bx]
  259. _DL    equ   es:byte ptr 08[bx]
  260. _DH    equ   es:byte ptr 09[bx]
  261.  
  262. _DS    equ   es:word ptr 10[bx]
  263.  
  264. _DI    equ   es:word ptr 12[bx]
  265. _SI    equ   es:word ptr 14[bx]
  266. _BP    equ   es:word ptr 16[bx]
  267.  
  268. _IP    equ   es:word ptr 18[bx]
  269. _CS    equ   es:word ptr 20[bx]
  270.  
  271. _PSW    equ   es:word ptr 22[bx]
  272.  
  273. _SP    equ   es:word ptr 24[bx]
  274. _SS    equ   es:word ptr 26[bx]
  275. #endasm
  276.  
  277.  
  278.